<template>
  <section class="welfarestatisticstwo-main">
    <el-collapse-transition>
      <div v-show="searchshow" class="course-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.giveInfoName"
          placeholder="请输入福利活动名称"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
          <span class="course-search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchshow" class="course-search separate">
        <div class="course-search-inner">
          <el-row>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">福利名称</div>
                <el-input
                  v-model.trim="search.giveInfoName"
                  style="width: 80%"
                  class="filter-item"
                  placeholder="请输入福利名称搜索"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">活动开始时间</div>
                <el-date-picker
                  v-model="search.startTime"
                  style="padding-left:30px ;"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="选择开始时间"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">活动结束时间</div>
                <el-date-picker
                  v-model="search.endTime"
                  style="padding-left:30px ;"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="选择结束时间"
                />

              </div>
            </el-col>
          </el-row>
        </div>
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
          <span class="course-search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>福利统计</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="exportFun">导出</el-button>
      </section>
    </section>

    <section class="main-table">
      <v-table
        :list="list"
        tag="welfarestatistics"
        :hover-arr="['balance']"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
      >
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          min-width="100"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleView(scope.row)">查看详情</el-button>

          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
  </section>
</template>
<script>
import {
  queryPage,
  exportFile
} from './../api'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
import { exportSign } from '@/utils/fileImportOrExportSign'
export default {
  name: 'Welfarestatisticstwo',
  components: {
    VTable,
    Pagination
  },
  directives: {
    elDragDialog
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        giveInfoName: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      searchshow: true,
      search: {
        giveInfoName: '',
        startTime: '',
        endTime: ''
      }
    }
  },
  watch: {

  },
  created() {
    this.getList()
    for (const key in this.dict) {
      this.getDict(key)
    }
  },
  methods: {
    handleView(row) {
      const fromObj = {
        from: '/datastatistics/welfarestatisticstwo/index'
      }
      // sessionStorage.setItem("curPath","/datastatistics/welfarestatisticstwo/")
      this.$router.push({
        path: this.$filterPath('activitystatistics'),
        query: {
          id: row.id,
          from: JSON.stringify(fromObj),
          c: row.giveInfoType,
          o: '1'
        }
      })
    },
    exportFun() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      var query = {
        page: this.listQuery.page,
        pageSize: this.listQuery.limit
        // giveName: this.listQuery.giveInfoName
      }
      if (!this.searchshow) {
        query['giveName'] = this.search.giveInfoName ? this.search.giveInfoName : ''
        query['startTime'] = this.search.startTime ? this.search.startTime + ' 00:00:00' : ''
        query['endTime'] = this.search.endTime ? this.search.endTime + ' 23:59:59' : ''
      } else {
        query['giveName'] = this.listQuery.giveInfoName
      }
      const obj = exportSign(query).obj
      exportFile(obj).then((res) => {
        loading.close()
        const content = res.data
        const blob = new Blob([content])
        const fileName = '福利统计信息.xlsx'
        if ('download' in document.createElement('a')) {
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else {
          navigator.msSaveBlob(blob, fileName)
        }
      }).catch(() => {
        loading.close()
      })
    },
    filtercolumn(scope, item) {
      if (item === 'giveInfoType') {
        return this.getFlName(scope.row[item])
      } else if (item === 'mall') {
        return this.getMall(scope.row['giveInfoType'])
      } else {
        return scope.row[item]
      }
    },
    getMall(t) {
      let name = '平台商城'
      if (t == '2') {
        name = '京东商城'
      } else if (t == '3' || t == '4' || t == '5' || t == '6') {
        name = '自有商城'
      }
      return name
    },
    getFlName(t) {
      let name = '福利包'
      switch (t) {
        case '1':
          name = '兑换券'
          break
        case '4':
          name = '兑换券'
          break
        case '5':
          name = '线下提货券'
          break
        case '6':
          name = '线下福利包'
          break
        case '7':
          name = '线下提货券'
          break
      }
      return name
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.giveInfoName = ''
      for (const k in this.search) {
        this.search[k] = ''
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: this.listQuery.page,
        pageSize: this.listQuery.limit,
        giveName: this.listQuery.giveInfoName
      }
      if (!this.searchshow) {
        query['giveName'] = this.search.giveInfoName ? this.search.giveInfoName : ''
        query['startTime'] = this.search.startTime ? this.search.startTime + ' 00:00:00' : ''
        query['endTime'] = this.search.endTime ? this.search.endTime + ' 23:59:59' : ''
      } else {
        query['giveName'] = this.listQuery.giveInfoName
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
	$dark_gray: #bfced6;

	.welfarestatisticstwo-main {
		padding: 10px;

		.el-dialog .el-dialog__body {
			max-height: 73vh !important;
		}

		.line {
			text-align: center;
		}

		.course-search {
			background-color: #fff;
			padding: 0 10px;

			.svg-container {
				padding: 0 5px 0 10px;
				color: $dark_gray;
				vertical-align: middle;
				display: inline-block;

				&_login {
					font-size: 20px;
				}
			}

			.search-input {
				width: 60%;
				position: relative;

				&:after {
					content: "";
					position: absolute;
					left: -25px;
					right: 0;
					bottom: 8px;
					height: 1px;
					background: rgba(226, 227, 232, 1);
				}

				input {
					outline: none;
					background: transparent;
					border: none;
					outline: medium;
					padding: 0;

					&:focus {
						outline: none;
						background-color: transparent;
					}
				}
			}

			.course-search-btn {
				display: inline-block;
				float: right;

				.el-button.is-round {
					padding: 10px 30px;
					margin-right: 34px;
				}

				.course-search-more {
					font-size: 14px;
					font-weight: 500;
					@include font-color($mainColor);
					cursor: pointer;
					margin-left: 10px;
				}
			}

			&.merge {
				height: 50px;
				line-height: 50px;
			}

			&.separate {
				.course-search-inner {
					padding-top: 10px;
						// .el-input--medium .el-input__inner {
						// 	padding-left: 30px !important;
						// }
					.search-inner-item {
						.item-label {
							height: 30px;
							line-height: 30px;
							font-size: 14px;
							color: #a6a6a6;
							font-weight: 400;
						}

						.el-input {
							position: relative;

							.el-input__inner {
								border: none;
								padding: 0;
							}

							&:after {
								content: "";
								height: 1px;
								background-color: #e2e3e8;
								position: absolute;
								left: 0;
								right: 0;
								bottom: 0;
							}
						}
					}
				}

				.course-search-btn {
					text-align: center;
					display: block;
					float: none;
					height: 56px;
					line-height: 56px;
				}
			}
		}

		.main-buttons {
			display: flex;
			line-height: 36px;
			margin: 10px 0;

			.main-buttons-title {
				flex: 1;
				height: 36px;
				line-height: 36px;
				font-size: 16px;
				font-weight: bold;
				color: rgba(51, 51, 51, 1);

				.num {
					@include font-color($mainColor);
				}
			}
		}
	}
</style>
